using Lichtspiel.Display;
using Lichtspiel.Math;

namespace Lichtspiel.ECSystem
{
    public class ImageRenderComponent : RenderComponent
    {

        private Image image;
        public Image Image { 
            get {
                return image;
            }

            set {
                this.image = value;
                this.SourceRectangle = image.Rectangle;
            }
        }

        public Rectangle SourceRectangle {
            get; set;
        }

         // TODO parenting safety (DisplayX)

        /// <summary>
        /// Gets the DisplayRectangle (scale taken into account)
        /// </summary>
        public Rectangle DisplayRectangle { // TODO displayrect testen
            get {
                Vector2 anchor = MathHelper.AnchorToVector(Owner.Anchor, new SizeF(SourceRectangle.Width, SourceRectangle.Height));
                return new Rectangle((int) (Owner.Position.X - anchor.X * Owner.Scale.X),
                                     (int) (Owner.Position.Y - anchor.Y * Owner.Scale.Y), (int) DisplaySize.Width,
                                     (int) DisplaySize.Height);
            }
        }

        /// <summary>
        /// Gets the DisplaySize (scale taken into account)
        /// </summary>
        public SizeF DisplaySize {
            get {
                return new SizeF(this.SourceRectangle.Width * Owner.Scale.X, this.SourceRectangle.Height * Owner.Scale.Y);
            }
        }



        public BlendMode BlendMode {
            get;
            set;
        }

        public ImageRenderComponent(Image image) {
            this.Image = image;
            this.BlendMode = Display.BlendMode.Alpha;            
        }

        public override void Render() {
            Vector2 anchor = MathHelper.AnchorToVector(Owner.Anchor, new SizeF(SourceRectangle.Width, SourceRectangle.Height));
            Image.Draw(Owner.Position - anchor, Owner.Color, Owner.Rotation, anchor, Owner.Scale, anchor, this.SourceRectangle, this.BlendMode);
        }
    }
}
